home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / tcp_ip / wnos / wn941101 / udp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-10  |  6.3 KB  |  272 lines

  1. /* Send and receive User Datagram Protocol packets */
  2. #include "global.h"
  3. #include "mbuf.h"
  4. #include "netuser.h"
  5. #include "iface.h"
  6. #include "udp.h"
  7. #include "ip.h"
  8. #include "internet.h"
  9. #include "icmp.h"
  10.  
  11. static struct udp_cb * near lookup_udp __ARGS((struct socket *socket));
  12.  
  13. struct mib_entry Udp_mib[] = {
  14.     "",            0,
  15.     "InDatagrams",    0,
  16.     "NoPorts",        0,
  17.     "InErrors",        0,
  18.     "OutDatagrams",    0,
  19. };
  20.  
  21. /* UDP control structures list */
  22. struct udp_cb *Udps;
  23.  
  24. /* Create a UDP control block for lsocket, so that we can queue
  25.  * incoming datagrams.
  26.  */
  27. struct udp_cb *
  28. open_udp(lsocket,r_upcall)
  29. struct socket *lsocket;
  30. void (*r_upcall)();
  31. {
  32.     struct udp_cb *up;
  33.  
  34.     if((up = lookup_udp(lsocket)) != NULLUDP){
  35.         /* Already exists */
  36.         Net_error = CON_EXISTS;
  37.         return NULLUDP;
  38.     }
  39.     up = (struct udp_cb *)mxallocw(sizeof (struct udp_cb));
  40.     up->socket.address = lsocket->address;
  41.     up->socket.port = lsocket->port;
  42.     up->r_upcall = r_upcall;
  43.  
  44.     up->next = Udps;
  45.     Udps = up;
  46.     return up;
  47. }
  48.  
  49. /* Send a UDP datagram */
  50. int
  51. send_udp(lsocket,fsocket,tos,ttl,data,length,id,df)
  52. struct socket *lsocket;        /* Source socket */
  53. struct socket *fsocket;        /* Destination socket */
  54. char tos;            /* Type-of-service for IP */
  55. char ttl;            /* Time-to-live for IP */
  56. struct mbuf *data;        /* Data field, if any */
  57. int16 length;            /* Length of data field */
  58. int16 id;            /* Optional ID field for IP */
  59. char df;            /* Don't Fragment flag for IP */
  60. {
  61.     struct mbuf *bp;
  62.     struct pseudo_header ph;
  63.     struct udp udp;
  64.     int32 laddr;
  65.  
  66.     if(length != 0 && data != NULLBUF)
  67.         trim_mbuf(&data,length);
  68.     else
  69.         length = len_p(data);
  70.  
  71.     length += UDPHDR;
  72.  
  73.     laddr = lsocket->address;
  74.     if(laddr == INADDR_ANY)
  75.         laddr = locaddr(fsocket->address);
  76.  
  77.     udp.source = lsocket->port;
  78.     udp.dest = fsocket->port;
  79.     udp.length = length;
  80.  
  81.     /* Create IP pseudo-header, compute checksum and send it */
  82.     ph.length = length;
  83.     ph.source = laddr;
  84.     ph.dest = fsocket->address;
  85.     ph.protocol = UDP_PTCL;
  86.  
  87.     if((bp = htonudp(&udp,data,&ph)) == NULLBUF){
  88.         Net_error = NO_MEM;
  89.         free_p(data);
  90.         return 0;
  91.     }
  92.     udpOutDatagrams++;
  93.     ip_send(laddr,fsocket->address,UDP_PTCL,tos,ttl,bp,length,id,df);
  94.     return (int)length;
  95. }
  96. /* Accept a waiting datagram, if available. Returns length of datagram */
  97. int
  98. recv_udp(up,fsocket,bp)
  99. struct udp_cb *up;
  100. struct socket *fsocket;        /* Place to stash incoming socket */
  101. struct mbuf **bp;        /* Place to stash data packet */
  102. {
  103.     struct socket sp;
  104.     struct mbuf *buf;
  105.     int16 length;
  106.  
  107.     if(up == NULLUDP){
  108.         Net_error = NO_CONN;
  109.         return -1;
  110.     }
  111.     if(up->rcvcnt == 0){
  112.         Net_error = WOULDBLK;
  113.         return -1;
  114.     }
  115.     buf = dequeue(&up->rcvq);
  116.     up->rcvcnt--;
  117.  
  118.     /* Strip socket header */
  119.     pullup(&buf,(char *)&sp,sizeof(struct socket));
  120.  
  121.     /* Fill in the user's foreign socket structure, if given */
  122.     if(fsocket != NULLSOCK){
  123.         fsocket->address = sp.address;
  124.         fsocket->port = sp.port;
  125.     }
  126.     /* Hand data to user */
  127.     length = len_p(buf);
  128.     if(bp != NULLBUFP)
  129.         *bp = buf;
  130.     else
  131.         free_p(buf);
  132.     return (int)length;
  133. }
  134.  
  135. /* Delete a UDP control block */
  136. int
  137. del_udp(conn)
  138. struct udp_cb *conn;
  139. {
  140.     struct mbuf *bp;
  141.     struct udp_cb *up, *udplast = NULLUDP;
  142.  
  143.     for(up = Udps;up != NULLUDP;udplast = up,up = up->next){
  144.         if(up == conn)
  145.             break;
  146.     }
  147.     if(up == NULLUDP){
  148.         /* Either conn was NULL or not found on list */
  149.         Net_error = INVALID;
  150.         return -1;
  151.     }
  152.     /* Get rid of any pending packets */
  153.     while(up->rcvcnt != 0){
  154.         bp = up->rcvq;
  155.         up->rcvq = up->rcvq->anext;
  156.         free_p(bp);
  157.         up->rcvcnt--;
  158.     }
  159.     /* Remove from list */
  160.     if(udplast != NULLUDP)
  161.         udplast->next = up->next;
  162.     else
  163.         Udps = up->next;    /* was first on list */
  164.  
  165.     xfree((char *)up);
  166.     return 0;
  167. }
  168.  
  169. /* Process an incoming UDP datagram */
  170. void
  171. udp_input(iface,ip,bp,rxbroadcast)
  172. struct iface *iface;    /* Input interface */
  173. struct ip *ip;        /* IP header */
  174. struct mbuf *bp;    /* UDP header and data */
  175. int rxbroadcast;    /* The only protocol that accepts 'em */
  176. {
  177.     struct pseudo_header ph;
  178.     struct udp udp;
  179.     struct udp_cb *up;
  180.     struct socket lsocket, fsocket;
  181.     struct mbuf *packet;
  182.     int16 length;
  183.  
  184.     if(bp == NULLBUF)
  185.         return;
  186.  
  187.     /* Create pseudo-header and verify checksum */
  188.     ph.source = ip->source;
  189.     ph.dest = ip->dest;
  190.     ph.protocol = ip->protocol;
  191.     length = ip->length - IPLEN - ip->optlen;
  192.     ph.length = length;
  193.  
  194.     /* Peek at header checksum before we extract the header. This
  195.      * allows us to bypass cksum() if the checksum field was not
  196.      * set by the sender.
  197.      */
  198.     udp.checksum = udpcksum(bp);
  199.     if(udp.checksum != 0 && cksum(&ph,bp,length) != 0){
  200.         /* Checksum non-zero, and wrong */
  201.         udpInErrors++;
  202.         free_p(bp);
  203.         return;
  204.     }
  205.     /* Extract UDP header in host order */
  206.     if(ntohudp(&udp,&bp) != 0){
  207.         /* Truncated header */
  208.         udpInErrors++;
  209.         free_p(bp);
  210.         return;
  211.     }
  212.     /* If this was a broadcast packet, pretend it was sent to us */
  213.     lsocket.address = (rxbroadcast) ? iface->addr : ip->dest;
  214.  
  215.     lsocket.port = udp.dest;
  216.     /* See if there's somebody around to read it */
  217.     if((up = lookup_udp(&lsocket)) == NULLUDP){
  218.         /* Nope, return an ICMP message */
  219.         if(!rxbroadcast){
  220.             bp = htonudp(&udp,bp,&ph);
  221.             icmp_output(ip,bp,ICMP_DEST_UNREACH,ICMP_PORT_UNREACH,NULLICMP);
  222.         }
  223.         udpNoPorts++;
  224.         free_p(bp);
  225.         return;
  226.     }
  227.     /* Create space for the foreign socket info */
  228.     if((packet = pushdown(bp,sizeof(fsocket))) == NULLBUF){
  229.         /* No space, drop whole packet */
  230.         free_p(bp);
  231.         udpInErrors++;
  232.         return;
  233.     }
  234.     fsocket.address = ip->source;
  235.     fsocket.port = udp.source;
  236.     memcpy(&packet->data[0],(char *)&fsocket,sizeof(fsocket));
  237.  
  238.     /* Queue it */
  239.     enqueue(&up->rcvq,packet);
  240.     up->rcvcnt++;
  241.     udpInDatagrams++;
  242.     if(up->r_upcall)
  243.         (*up->r_upcall)(iface,up,up->rcvcnt);
  244. }
  245.  
  246. /* Look up UDP socket.
  247.  * Return control block pointer or NULLUDP if nonexistant
  248.  * As side effect, move control block to top of list to speed future
  249.  * searches.
  250.  */
  251. static struct udp_cb * near
  252. lookup_udp(struct socket *socket)
  253. {
  254.     struct udp_cb *up, *uplast = NULLUDP;
  255.  
  256.     for(up = Udps;up != NULLUDP;uplast = up,up = up->next){
  257.         if(socket->port == up->socket.port
  258.           && (socket->address == up->socket.address
  259.           || up->socket.address == INADDR_ANY)){
  260.             if(uplast != NULLUDP){
  261.                 /* Move to top of list */
  262.                 uplast->next = up->next;
  263.                 up->next = Udps;
  264.                 Udps = up;
  265.             }
  266.             return up;
  267.         }
  268.     }
  269.     return NULLUDP;
  270. }
  271.  
  272.